package com.example.administrator.fastdream.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.example.administrator.fastdream.date.utils.EventBusCode;
import com.example.administrator.fastdream.date.utils.EventBusUtil;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;


public class PushReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            //收到自定义消息
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {     //自定义消息可以发送json数据
            System.out.println("收到了自定义消息。消息内容是：" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            // 自定义消息不会展示在通知栏，完全要开发者写代码去处理
            try {
                JSONObject jsonObject = new JSONObject(bundle.getString(JPushInterface.EXTRA_MESSAGE));
                if (jsonObject.optString("type").equals("1") || jsonObject.optString("type").equals("2")) {
                    EventBusUtil.postEvent(EventBusCode.MainF5);
                } else {
                    EventBusUtil.postEvent(EventBusCode.MainOpenRed);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            System.out.println("收到了通知");
            android.util.Log.e("alertsadasd", bundle.getString(JPushInterface.EXTRA_EXTRA));
            // 在这里可以做些统计，或者做些其他工作(通知栏被点击)
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            System.out.println("用户点击打开了通知");
        } else if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
        } else {
        }
    }
}
